<?php
/**
 * Created by PhpStorm.
 * User: bzing
 * Date: 2017/7/28 0028
 * Time: 上午 11:01
 */
namespace app\api\controller;
use app\models\Models;

class Voucher extends Base{
    /**
     * 我的代金券列表
     * @return \think\response\Json
     */
    public function voucherList(){
        if(request()->isPost()){
            try{
                // 登录验证
                $params = self::checkLogin(['equipment','encrypt','page']);
                $issue = new Models('voucher_issue');
                $where = ['a.uid'=>$params['uid'],'a.status'=>Models::VOUCHER_USE_NOT,'b.status'=>Models::VOUCHER_ISSUE_YES];
                $list = $issue->joinMany([['voucher b','a.voucher_id = b.id','left']],$where,'b.id,b.voucher_no,b.price,b.goods,b.type_id,b.is_platform,b.begin_time,b.end_time','b.begin_time asc');
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }

    /**
     * 可用代金券列表
     * @return \think\response\Json
     */
    public function validVoucherList(){
        if(request()->isPost()){
            try{
                // 登录验证
                $params = self::checkLogin(['equipment','encrypt','business_id','page']);
                $issue = new Models('voucher_issue');
                $where = ['a.uid'=>$params['uid'],'a.status'=>Models::VOUCHER_USE_NOT,'b.status'=>Models::VOUCHER_ISSUE_YES,'b.bid'=>[['eq',0],['eq',$params['business_id']],'or']];
                $list = $issue->joinMany([['voucher b','a.voucher_id = b.id','left']],$where,'b.id,b.voucher_no,b.price,b.goods,b.type_id,b.is_platform,b.begin_time,b.end_time','b.begin_time asc');
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }
}